package day_17.ChengYuanBianLiang;

import java.lang.reflect.Field;

public class ReflectDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        //m1();
        //m2();
        //m3();
        m4();
    }

    private static void m4() throws ClassNotFoundException, NoSuchFieldException {
        //        Field getDeclaredField(String name)：返回单个成员变量对象
        //1.获取class对象
        Class aClass = Class.forName("day_17.ChengYuanBianLiang.Student");
        //2.获取money成员变量
        Field field = aClass.getDeclaredField("money");
        //3.打印一下
        System.out.println(field);
    }

    private static void m3() throws ClassNotFoundException, NoSuchFieldException {
        //        Field getField(String name)：返回单个公共成员变量对象
        //想要获取的成员变量必须是真实存在的
        //且必须是public修饰的.
        //1.获取class对象
        Class aClass = Class.forName("day_17.ChengYuanBianLiang.Student");
        //2.获取name这个成员变量
        Field field = aClass.getField("name");
        //Field field = aClass.getField("name1");
        //Field field = aClass.getField("money");
        //3.打印一下
        System.out.println(field);
    }

    private static void m2() throws ClassNotFoundException {
        //        Field[] getDeclaredFields()：返回所有成员变量对象的数组
        //1.获取class对象
        Class<?> aClass = Class.forName("day_17.ChengYuanBianLiang.Student");
        //2.获取所有的Field对象
        Field[] field = aClass.getDeclaredFields();
        //3.遍历
        for (Field field1 : field) {
            System.out.println(field1);
        }
    }

    private static void m1() throws ClassNotFoundException {
        //        Field[] getFields()：返回所有公共成员变量对象的数组

        //1.获取class对象
        Class aClass = Class.forName("day_17.ChengYuanBianLiang.Student");
        //2.获取Field对象.
        Field[] fields = aClass.getFields();
        //3.遍历
        for (Field field : fields) {
            System.out.println(field);
        }
    }
}
class lx01{
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        //m1();
        //m2();
        //m3();
        m4();
    }

    private static void m4() throws ClassNotFoundException, NoSuchFieldException {
        Class aClass = Class.forName("day_17.ChengYuanBianLiang.Student");
        Field field = aClass.getDeclaredField("money");
        System.out.println(field);
    }

    private static void m3() throws ClassNotFoundException, NoSuchFieldException {
        Class aClass = Class.forName("day_17.ChengYuanBianLiang.Student");
        Field field = aClass.getField("name");
        System.out.println(field);
    }

    private static void m2() throws ClassNotFoundException {
        Class aClass = Class.forName("day_17.ChengYuanBianLiang.Student");
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }
    }

    private static void m1() throws ClassNotFoundException {
        Class aClass = Class.forName("day_17.ChengYuanBianLiang.Student");
        Field[] fields = aClass.getFields();
        for (Field field : fields) {
            System.out.println(field);

        }
    }
}
